home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / SystemProcess / ODXFuncs.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-13  |  7.9 KB  |  265 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OXFuncs.cpp
  3.  
  4.     Contains:    OpenDoc™ launcher code shared between old and new launcher
  5.  
  6.     Owned by:    Nick Pilch
  7.  
  8.     Copyright:    © 1996 - 1997 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <13>      1/8/97    DH        1401434: Properly check for read-only docs
  13.                                     in StartDocSizeChangeProcess.
  14.         <12>      12/18/96    DH        1375780: Now handles case of opening doc
  15.                                     with large doc size that is read only.
  16.         <11>      10/23/96    DH        Fixed up another build break problem.
  17.         <10>      10/23/96    DH        Added function prototype to fix problem
  18.                                     with previous checkin.
  19.          <9>      10/22/96    DH        1333949    • Misleading out of memory message
  20.                                     from sample parts. 1336808    • OpenDoc 1.0.4:
  21.                                     does not run when memory is set to high
  22.                                     value. Check if a document process couldn't
  23.                                     be started because of out-of-mem. If so,
  24.                                     start doc heap size change process via new
  25.                                     event to OD shell.
  26.          <8>     7/11/96    eeh        1363971: plug memory leaks
  27.          <7>     6/23/96    NP        10002: Launch time speedups.
  28.          <6>     6/14/96    NP        10002: Launch time speedups
  29.          <5>    .06.1996    NP        10002: Launch time fixes, system process.
  30.          <4>    .05.1996    NP        Reverting.
  31.          <3>    .05.1996    NP        10002: Launch time fixes.
  32.          <2>    .05.1996    NP        Revised HandleLaunchFailed.
  33.          <1>    .05.1996    NP        first checked in
  34.  
  35.     To Do:
  36.     In Progress:
  37.         
  38. */
  39.  
  40.  
  41. #ifndef _ODXFUNCS_
  42. #include "ODXFuncs.h"
  43. #endif
  44.  
  45. #ifndef _ODPRCS_
  46. #include "ODPrcs.h"
  47. #endif
  48.  
  49. //#ifndef _SPDEFS_
  50. //#include "SPDefs.h"
  51. //#endif
  52.  
  53. //#ifndef _SHELLDEF_
  54. //#include "ShellDef.h"
  55. //#endif
  56.  
  57. #ifndef _ODXDPFNS_
  58. #include "ODXDpFns.h"
  59. #endif
  60.  
  61. #ifndef __TOOLUTILS__
  62. #include <ToolUtils.h>
  63. #endif
  64.  
  65. #ifndef _SHELLDEF_
  66. #include "ShellDef.h"
  67. #endif
  68.  
  69. #ifndef __ICONS__
  70. #include <Icons.h>
  71. #endif
  72.  
  73. #ifndef __RESOURCES__
  74. #include <Resources.h>
  75. #endif
  76.  
  77. //==============================================================================
  78. // Macros
  79. //==============================================================================
  80.  
  81. #define CHECK(EXPR)    if((err=(EXPR)) == noErr) ; else return err
  82.  
  83. //==============================================================================
  84. // Function Prototypes
  85. //==============================================================================
  86.  
  87. OSErr StartDocSizeChangeProcess(FSSpec* theFSSpec,DescType evtID);
  88.  
  89. //------------------------------------------------------------------------------
  90. // HandleLaunchFailed
  91. //------------------------------------------------------------------------------
  92.  
  93. pascal OSErr HandleLaunchFailed( const AppleEvent* ae, AppleEvent* /*reply*/,
  94.                                     long /*refCon*/ )
  95. {
  96.      // This is an Apple Event handler that catches launch-failed events.
  97.     
  98.     long            launchError;
  99.     Str255            appName, libName;
  100.     
  101.     GetLaunchFailedParams(ae, launchError, NULL, appName, libName);
  102.     
  103.     // THIS HAPPENS IN THE NORMAL COURSE OF QUITTING A PROCESS
  104. //    if( launchError == noErr )
  105. //        WARN("HandleLaunchFailed: Got an app-died event, but error is noErr.");
  106.  
  107.     if (launchError != noErr)
  108.         NotifyLaunchFailed(launchError, appName, libName);
  109.  
  110.     return noErr;
  111. }
  112.  
  113. //------------------------------------------------------------------------------
  114. // HandleRequiredEvent
  115. //------------------------------------------------------------------------------
  116.  
  117. pascal OSErr HandleRequiredEvent(const AppleEvent *theAppleEvent,
  118.                                     AppleEvent* /*reply*/, long /*refCon*/)
  119. {
  120.     AEDescList    documentList = {typeNull, NULL};
  121.     long        documentCount;
  122.     OSErr        err = noErr;
  123.     FSSpec        theFSSpec;
  124.  
  125. //    GetOpenDocBinaryFileSpec(kODNULL); // Sets up CFM search paths.
  126.     
  127.     CHECK(AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList));
  128.     CHECK(AECountItems(&documentList,&documentCount));
  129.     DescType evtID;
  130.     DescType ignoreType;
  131.     Size ignoreActual;
  132.     CHECK( AEGetAttributePtr( theAppleEvent, keyEventIDAttr, typeType,
  133.             &ignoreType, &evtID, sizeof(evtID), &ignoreActual ));
  134.  
  135.     err = noErr;
  136.     
  137.     FSSpec        seedFSSpec;
  138.     seedFSSpec.name[0] = 0;
  139.  
  140.     AEDesc        documentDesc = {typeNull, NULL};
  141.     
  142.     while (documentCount && (err == noErr))
  143.     {
  144.         AEKeyword    theAEKeyword;
  145.         
  146.         CHECK(AEGetNthDesc(&documentList, documentCount, typeWildCard,
  147.                 &theAEKeyword, &documentDesc));
  148.         if( GetDescFSSpec(&documentDesc,theFSSpec) != noErr )
  149.             theFSSpec.name[0] = 0;
  150.         err = VerifyAndLaunchDocumentStub(&documentDesc, kODNULL,
  151.                 kODNULL, evtID, &seedFSSpec);
  152.         if( err == memFullErr ||  err == memFragErr)
  153.         {
  154.             err = StartDocSizeChangeProcess(&theFSSpec,evtID);
  155.         }
  156.         if ( --documentCount )
  157.             (void)AEDisposeDesc( &documentDesc );
  158.     }
  159.     (void)AEDisposeDesc( &documentList );
  160.  
  161.     if (seedFSSpec.name[0] != 0)
  162.         err= LaunchDocumentStub(&documentDesc, &seedFSSpec, kODNULL,
  163.                 kODNULL, kODNULL, evtID);
  164.     (void)AEDisposeDesc( &documentDesc );
  165.         
  166.     if (err)
  167.         ReportDocumentError(theFSSpec.name, err);
  168.     else
  169.         DocumentLaunched();
  170.  
  171.     return err;
  172. }
  173.  
  174. OSErr StartDocSizeChangeProcess(FSSpec* theFSSpec,DescType evtID)
  175. {
  176.     // This function deals with the problem where a document has a custom
  177.     // size which makes it too big to open in available memory. Because
  178.     // OpenDoc requires you to open a document to change its size, this 
  179.     // could lead to unopenable documents. So, we allow the user to change
  180.     // the size here and open the document again.
  181.     // To implement this, it launches the document like it would normally,
  182.     // but passes a different event that tells OpenDoc to let the user lower
  183.     // the size of the document. After finishing this, OpenDoc sends an
  184.     // event back to the System Process that closes and opens the document
  185.     // again.
  186.     OSErr err = noErr;
  187.  
  188.     ODHandle size0 = kODNULL;
  189.     ODHandle size1 = kODNULL;
  190.     ODSShort prefResFile = 0;
  191.     short     numSizeRsrcs = 0;
  192.     ODBoolean dontClose = kODFalse;
  193.     ODBoolean isReadOnly = kODFalse;
  194.     ODHandle  errString = kODNULL;
  195.     
  196.     // First, check to see if the file is read-only. If it is, the user cannot
  197.     // change the size, so put up an alert with some suggestions on how to open
  198.     // the document.
  199.     err = FileIsReadOnly(theFSSpec, &isReadOnly);
  200.     if( err== noErr )
  201.     {
  202.         if( isReadOnly )
  203.         {
  204.             errString = Get1Resource('STR ', kODSPStrCannotOpenRODocSizeErrMsg);
  205.             HLock( errString );
  206.             ReportErrorGeneric( (unsigned char*)*errString);
  207.             ReleaseResource(errString);
  208.         }
  209.         else
  210.         {
  211.             short theCurResFile = CurResFile();
  212.             prefResFile = FSpOpenResFile((const FSSpec *)theFSSpec,fsRdWrPerm);
  213.             
  214.             if (prefResFile != -1)
  215.             {
  216.                 numSizeRsrcs = Count1Resources(kSIZERsrcType);
  217.                 // If there is a preferred size resource, there will be SIZE 0 & 1
  218.                 // in addition to the SIZE -1 resource.
  219.                 if( numSizeRsrcs > 1 )
  220.                 {
  221.                     // Save current size of document in temp resource, and set size of
  222.                     // document to default minimum size.
  223.                     err =  ReplaceResourceWithNewType( kSIZERsrcType, kODTempSizeRsrcType, 0 );
  224.                     err =  ReplaceResourceWithNewType( kSIZERsrcType, kODTempSizeRsrcType, 1 );
  225.                 
  226.                     CloseResFile( prefResFile );
  227.                     UseResFile(theCurResFile);
  228.                     dontClose = kODTrue;
  229.                     
  230.                     // Create Apple Event of type "DocSizeDialog" to pass to LaunchApplication .
  231.                     AEAddressDesc myAddress;
  232.                     myAddress.descriptorType = 'null';
  233.                     myAddress.dataHandle = NULL;
  234.                     AppleEvent theEvent;
  235.                     err = AECreateAppleEvent( 'dumb', 'hack', &myAddress,
  236.                                         kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
  237.                     err = AEPutParamPtr( &theEvent, 'docs', typeFSS, theFSSpec, sizeof(FSSpec) );
  238.     
  239.                     AEDesc launchDesc;
  240.                     err = AECoerceDesc(&theEvent, typeAppParameters, &launchDesc);
  241.     
  242.                     HLock(launchDesc.dataHandle);
  243.                     LaunchParamBlockRec    launchPB;
  244.                     launchPB.launchBlockID = extendedBlock;
  245.                     launchPB.launchEPBLength = extendedBlockLen;
  246.                     launchPB.launchControlFlags = launchContinue | launchNoFileFlags | launchUseMinimum;
  247.                     launchPB.launchAppSpec = theFSSpec;
  248.                     launchPB.launchAppParameters = (AppParametersPtr)*(launchDesc.dataHandle);
  249.                     
  250.                     // Launch document process that will allow the user to change the size of the document.
  251.                     err = LaunchApplication(&launchPB);
  252.                 }
  253.                 if( dontClose == kODFalse )
  254.                 {
  255.                     CloseResFile( prefResFile );
  256.                     UseResFile(theCurResFile);
  257.                 }
  258.             }
  259.         }
  260.     }
  261.     return err;
  262. }
  263.  
  264.  
  265.